#pragma once


#include <iostream>
#include <memory>
#include <functional>
#include "Epoller.hpp"
#include "InetAddr.hpp"
#include "Socket.hpp"
#include "Log.hpp"
#include "Connection.hpp"
#include "Protocol.hpp"
#include "Reactor.hpp"


using func_t = std::function<std::string(std::string&)>;


//IOService只进行信息的获取
class IOService : public Connection
{
    static const int size = 1024;
public:
    IOService(int sockfd)
    {
        //1.设置文件描述符非阻塞
        SetNonBlock(sockfd);
        SetSockfd(sockfd);
        SetEvents(EPOLLIN | EPOLLET);
    }
    ~IOService()
    {

    }
    virtual void Sender() override
    {
        while(true)
        {
            ssize_t n = send(sockfd(),OutString().c_str(),OutString().size(),0);
            if(n > 0)
            {
                //成功
                DisCardOutString(n);//移除发送的n个字符串
            }
            else if(n == 0)
            {
                break;
            }
            else
            {
                if(errno == EAGAIN || errno == EWOULDBLOCK)
                {
                    //缓冲区写满了,下次再来写
                    break;
                }
                else if(errno == EINTR)
                {
                    continue;
                }
                else
                {
                    Excepter();
                    return;
                }
            }
        }
        //本轮发送完了
        //1.outbuffer empty
        //2.发送缓冲区写满了 && outbuffer 没有empty
        //写条件不满足,我们要将sockfd的事件写入到epoll模型中
        if(!IsOutBufferEmpty())
        {
            //修改对sockfd的事件关心!  --  开启对写事件的关心
            GetOwner()->EnableReadWrite(sockfd(),true,true);
        }
        else
        {
            GetOwner()->EnableReadWrite(sockfd(),true,false);
        }
    }
    virtual void Recver() override
    {
        //1.读取所有的数据
        while(true)
        {
            char buffer[size];
            ssize_t s = recv(sockfd(),buffer,sizeof(buffer) - 1,0);//非阻塞
            if(s > 0)
            {
                buffer[s] = 0;
                //读取成功
                Append(buffer);
            }
            else if(s == 0)
            {
                //对端关闭连接
                Excepter();
                return;
            }
            else
            {
                if(errno == EAGAIN || errno == EWOULDBLOCK)
                {
                    break;
                }
                else if(errno == EINTR)
                {
                    continue;
                }
                else
                {
                    Excepter();
                    return;
                }
            }
        }
        //走到这里我们肯定已经读取完了
        std::cout << "inbuffer: \n" << InBuffer() << std::endl;
        //能确定读到的消息是一个完整报文吗? 不一定(TCP面向字节流)
        //要引入我们的协议
        std::string result;
        if(_on_message)
        {
            result = _on_message(InBuffer());
        }
        //添加应答信息
        AppendToOut(result);
        
        // outbuffer发送给对方
        if(!IsOutBufferEmpty())
        {
            //方案一: Sender(); //直接进行发送
            //方案二: 使能writeable即可
            GetOwner()->EnableReadWrite(sockfd(),true,true);
        }
    }
    void RegisterOnMessage(func_t on_message)
    {
        _on_message = on_message;
    }
    virtual void Excepter()override
    {
        //IO读取时,所有的异常处理,全部都会转化为这个函数
        //打印日志,差错处理,关闭连接,Reactor移除connection,删除epoll模型中的节点
        LOG(LogLevel::INFO) << "客户端可能结束,进行异常处理: " << sockfd();
        GetOwner()->DelConnection(sockfd());
    }
private:
    func_t _on_message;
};